﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace HeathCareSystem
{
    public class BenhNhanTrongNgayDAO
    {
        static DataProvider dp = new DataProvider();

        public bool ThemBenhNhanTrongNgayMoi(BenhNhanTrongNgayDTO bntnDTO)
        {
            string strSQL = string.Format("INSERT INTO BenhNhanTrongNgay (MaSoKhamBenh, MaBenhNhan, MaBacSi, MaChiTietBenhAn) "+
                                          " VALUES ('{0}', '{1}', '{2}', {3})", bntnDTO.maSoKhamBenh, bntnDTO.maBenhNhan, bntnDTO.maBacSi, bntnDTO.maChiTietBenhAn);
            return dp.ExecuteNonQuery(strSQL);
        }

        public bool XoaDanhSachBenhNhanTrongNgay()
        {
            string strSQL = string.Format("DELETE BenhNhanTrongNgay");
            return dp.ExecuteNonQuery(strSQL);
        }

        public int TaoMaSoKhamBenhTuDong()
        {
            // Chuỗi cố định trong mã phiếu khám bệnh
            int iChu;
            int iMaDau = 1;

            String strSql = "SELECT TOP 1 MaSoKhamBenh FROM BENHNHANTRONGNGAY ORDER BY MaSoKhamBenh DESC";

            try
            {
                int iTemp = int.Parse(dp.ExecuteScalar(strSql).ToString());
                int iMaCuoi;
                if (iTemp.Equals(""))
                {
                    iMaCuoi = iTemp;
                    return iMaCuoi;
                }
                else
                {
                    iMaCuoi = iTemp + 1;
                    return iMaCuoi;
                } 

            }
            catch (Exception)
            {
                return iMaDau;
            }
        }

        public static List<BenhNhanTrongNgayDTO> TaoDanhSachDayDuThongTin(String strSQL)
        {
            List<BenhNhanTrongNgayDTO> lst = new List<BenhNhanTrongNgayDTO>();
            try
            {
                DataTable dt = new DataTable();
                dt = dp.ExecuteQuery(strSQL);
                foreach (DataRow dr in dt.Rows)
                {
                    BenhNhanTrongNgayDTO bn = new BenhNhanTrongNgayDTO();
                    bn.maSoKhamBenh = dr["MaSoKhamBenh"].ToString();
                    bn.maBenhNhan = dr["MaBenhNhan"].ToString();
                    bn.maBacSi = dr["MaBacSi"].ToString();
                    bn.maChiTietBenhAn = int.Parse(dr["MaChiTietBenhAn"].ToString());

                    BenhNhanDTO benhnhan = new BenhNhanDTO();
                    benhnhan.hoTen = dr["HoTen"].ToString();
                    benhnhan.gioiTinh = dr["GioiTinh"].ToString();
                    benhnhan.diaChi = dr["DiaChi"].ToString();
                    benhnhan.ngaySinh = DateTime.Parse(dr["NgaySinh"].ToString());
                    benhnhan.soDienThoai = dr["SoDienThoai"].ToString();
                    benhnhan.queQuan = dr["QueQuan"].ToString();
                    benhnhan.tinhTrangHonNhan = dr["TinhTrangHonNhan"].ToString();
                    bn.BenhNhan = benhnhan;
                    lst.Add(bn);
                }
            }
            catch (Exception)
            {
                lst = null;
            }
            return lst;
        }
        public static List<BenhNhanTrongNgayDTO> TaoDanhSach(String strSQL)
        {
            List<BenhNhanTrongNgayDTO> lst = new List<BenhNhanTrongNgayDTO>();
            try
            {
                DataTable dt = new DataTable();
                dt = dp.ExecuteQuery(strSQL);
                foreach (DataRow dr in dt.Rows)
                {
                    BenhNhanTrongNgayDTO bn = new BenhNhanTrongNgayDTO();
                    bn.maSoKhamBenh = dr["MaSoKhamBenh"].ToString();
                    bn.maBenhNhan = dr["MaBenhNhan"].ToString();
                    bn.maBacSi = dr["MaBacSi"].ToString();
                    bn.maChiTietBenhAn = int.Parse(dr["MaChiTietBenhAn"].ToString());
                    lst.Add(bn);
                }
            }
            catch (Exception)
            {
                lst = null;
            }
            return lst;
        }
        public static List<BenhNhanTrongNgayDTO> DanhSachBenhNhanTrongNgay()
        {
            string strSQL = "SELECT BenhNhanTrongNgay.*, BenhNhan.MaBenhNhan, BenhNhan.HoTen, BenhNhan.GioiTinh, BenhNhan.NgaySinh, BenhNhan.DiaChi, BenhNhan.SoDienThoai, BenhNhan.QueQuan, BenhNhan.TinhTrangHonNhan, BenhNhan.MaTaiKhoan FROM BenhNhan INNER JOIN " +
                          " BenhNhanTrongNgay ON BenhNhan.MaBenhNhan = BenhNhanTrongNgay.MaBenhNhan AND BenhNhan.MaBenhNhan = BenhNhanTrongNgay.MaBenhNhan";
            return TaoDanhSachDayDuThongTin(strSQL);

        }
        public static BenhNhanTrongNgayDTO LayThongTinTheoMa(string ma)
        {
            string strSQL = string.Format("SELECT * FROM BenhNhanTrongNgay WHERE MaSoKhamBenh = '{0}'", ma);
            return TaoDanhSach(strSQL)[0];
        }
    }
}